package org.opendatakit.sensors.wifi;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.UnknownHostException;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;


public class SenderThread implements Runnable{

public static final int DGRAM_LENGTH = 1024;
public static final int PORT = 5555;

DatagramSocket ds;
String message;
String ip;
Context context;
//MulticastService multicast;
public SenderThread(String cmd,Context c, String ip){
	this.ip=ip;
	context=c;
	this.message = cmd;
	try {
			ds = new DatagramSocket();
			Log.d("MST", ds.toString());

	} catch (IOException e) {
		e.printStackTrace();
	}
	
	
	
}

@SuppressWarnings("deprecation")
public void run() {

	byte buf[] = new byte[1024];
	DatagramPacket pack;
	buf=message.getBytes();
	
	try {
		pack = new DatagramPacket(buf, buf.length, InetAddress.getByName(ip), PORT);
		ds.send(pack);
		Log.d("Sent","Sent data to: " + pack.getAddress().toString() + ":" + pack.getPort() + " with length: " +
			    pack.getLength());
	} catch (UnknownHostException e2) {
		// TODO Auto-generated catch block
		e2.printStackTrace();
	}//	
	catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	
	
	
	
}

}